<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <style>
        .div1 {
            display: inline-block;
            width: 670px;
            height: 320px;
            padding: 10px;
            overflow: hidden;
            overflow-y: auto;
            vertical-align: top;
        }
    </style>
    <meta charset="UTF-8">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css">
    <script type="text/javascript" src="/lib/zTree_v3/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="/lib/zTree_v3/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="/lib/zTree_v3/js/jquery.ztree.excheck.js"></script>
    <script type="text/javascript">

        var setting = {
            check: {
                enable: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "", "N": "" }
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "departmentCode",
                    pIdKey: "pdepartmentCode"
                },
                key: {
                    name: "departmentName"
                },
            },
            callback: {
                beforeCheck: true,
                onCheck: onCheck,
            }
        };

        function selfsetting() {
            var treeObj = $.fn.zTree.getZTreeObj("treeMultiple");//获取到树
            var nodesSys = treeObj.getNodes(); //可以获取所有的父节点
            var nodes = treeObj.transformToArray(nodesSys); //获取树所有节点
            for (var i = 0; i < nodes.length; i++) {
                nodes[i].isParent = true;
                treeObj.updateNode(nodes[i]);
            }
        }

        //从后台取部门的值
        function getTree() {
            var tree = null;
            var ss = parent.config
            $.ajax({
                url: ss.server + "/admin/tree/getDeptTree",
                type: "get",
                contentType: "application/json",
                timeout: 30000, //超时时间：30秒
                async: false,//false-同步（当这个ajax执行完后才会继续执行其他代码）；异步-与其他代码互不影响，一起运行。
                dataType: "json",
                success: function (data) {
                    tree = data.data;
                }, error: function (data) {
                }
            });
            return tree;
        }

        $(document).ready(function () {
            var treenode = $.fn.zTree.init($("#treeMultiple"), setting, getTree());
            treenode.expandAll(true);
            selfsetting();
            var kk = parent.$('#chooseDepartmentNo').val().split(';');
            var treeObj = $.fn.zTree.getZTreeObj("treeMultiple");//获取到树
            var nodesSys = treeObj.getNodes(); //可以获取所有的父节点
            var nodes = treeObj.transformToArray(nodesSys); //获取树所有节点
            for (var i = 0; i < nodes.length; i++) {
                for (var j = 0; j < kk.length; j++) {
                    if (nodes[i].departmentCode == kk[j]) {
                        treenode.checkNode(nodes[i]);//勾选结点
                        break;
                    }
                }
            }
        });

        function onCheck(e, treeId, treeNode) {
            var treeObj = $.fn.zTree.getZTreeObj("treeMultiple"),
                nodes = treeObj.getCheckedNodes(true),
                list = [];
            for (var i = 0; i < nodes.length; i++) {
                list.push({ "部门编号": nodes[i].departmentCode, "部门名称": nodes[i].departmentName })
            }
            var resName = ""
            var resNo = ""
            for (var index = 0; index < list.length; index++) {
                var name = list[index].部门名称 + ";";
                resName += name;
                var no = list[index].部门编号 + ";";
                resNo += no
            }
            parent.$('#chooseDepartment').val(resName.substring(0, resName.lastIndexOf(";")));
            parent.$('#chooseDepartmentNo').val(resNo.substring(0, resNo.lastIndexOf(";")));
            return list;
        };


        function save() {
            var index = parent.layer.getFrameIndex(window.name);
            if (parent.$('#chooseDepartment').val() == "") {
                alert("请选择目标部门")
            } else {
                parent.layer.close(index);
            }
        }

    </script>

</head>

<body>
    <div class="layui-form-item">
        <div class="div1">
            <ul id="treeMultiple" class="ztree" style=" overflow-y: auto"></ul>
        </div>
    </div>
   
    <div>
        <div class="layui-input-block">&nbsp;&nbsp;&nbsp;&nbsp;
            <button class="layui-btn"
                style="position:absolute;right:6%;bottom:4%;width:80px;height:30px;border:none;outline:none;background-color:#1E9FFF;color:white"
                onclick="save()">确认</button>
        </div>
    </div>
</body>

</html>